package icraf.carbon.gui.chart;

import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_XSCALE_HA;
import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_XSCALE_TOTAL;
import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_YSCALE_LOG;
import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_YSCALE_NORMAL;
import icraf.carbon.core.CostBenefitUnit;
import icraf.carbon.core.Project;
import icraf.carbon.gui.MenuFactory.MenuProperty;
import icraf.common.gui.MenuAdapter;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

public class DisplaySettingPanel extends JPanel {
	class ComboBoxRenderer extends DefaultListCellRenderer {
		private static final long serialVersionUID = 1799773038253861399L;

		@Override
		public Component getListCellRendererComponent(JList<?> list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {
			JLabel rend = (JLabel) super.getListCellRendererComponent(list,
					value, index, isSelected, cellHasFocus);
			if (value instanceof MenuProperty) {
				rend.setText(((MenuProperty) value).getLabel());
			}
			return rend;
		}
	}

	private static final long serialVersionUID = 8276299503836967050L;
	private JComboBox<CostBenefitUnit> cbuCB;
	private DefaultComboBoxModel<CostBenefitUnit> cbuModelCB = new DefaultComboBoxModel<CostBenefitUnit>();
	private JCheckBox eligibleCB;
	private MenuAdapter[] menuXScale = { CHART_XSCALE_HA, CHART_XSCALE_TOTAL };
	private MenuAdapter[] menuYScale = { CHART_YSCALE_LOG, CHART_YSCALE_NORMAL };
	private Project project;
	private JComboBox<MenuAdapter> unitXCB;
	private JFormattedTextField unitXMax;
	private JFormattedTextField unitXMin;
	private JComboBox<MenuAdapter> unitYCB;
	private JFormattedTextField unitYMax;
	private JFormattedTextField unitYMin;
	private JFormattedTextField costF;
	private NumberFormat numberFormat;

	public DisplaySettingPanel() {
		numberFormat = NumberFormat.getNumberInstance();
		numberFormat.setMaximumFractionDigits(32);
		
		eligibleCB = new JCheckBox(MenuProperty.CHART_ELI_ONLY.getLabel());
		cbuCB = new JComboBox<CostBenefitUnit>(cbuModelCB);
		costF = new JFormattedTextField(numberFormat);
		costF.setColumns(8);
		
		JLabel cbuL = new JLabel(MenuProperty.PROJECT_COSTB_UNIT.getLabel());
		JLabel costL = new JLabel(MenuProperty.PROJECT_SUMMARY_THRESHOLD.getLabel());

		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints(); 

		c.insets = new Insets(5, 5, 5, 5);
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.WEST;
		c.gridwidth = 2;
		c.gridx = 0;
		c.gridy = 0;

		this.add(eligibleCB, c);
		c.gridwidth = 1;
		c.gridx = 0;
		c.gridy = 1;
		this.add(cbuL, c);
		c.gridx = 1;
		c.gridy = 1;
		this.add(cbuCB, c);
		c.gridx = 0;
		c.gridy = 2;
		this.add(costL, c);
		c.gridwidth = 1;
		c.gridx = 1;
		c.gridy = 2;
		this.add(costF, c);
		c.gridwidth = 2;
		c.gridx = 0;
		c.gridy = 3;
		this.add(initAxisPanel(), c);

	}

	public CostBenefitUnit getCostBenefitUnit() {
		return (CostBenefitUnit) cbuCB.getSelectedItem();
	}

	public Project getProject() {
		return project;
	}

	public MenuProperty getUnitX() {
		return (MenuProperty) unitXCB.getSelectedItem();
	}

	public String getUnitXMax() {
		return unitXMax.getText();
	}

	public String getUnitXMin() {
		return unitXMin.getText();
	}

	public MenuProperty getUnitY() {
		return (MenuProperty) unitYCB.getSelectedItem();
	}

	public String getUnitYMax() {
		return unitYMax.getText();
	}

	public String getUnitYMin() {
		return unitYMin.getText();
	}

	private JPanel initAxisPanel() {
		unitXCB = new JComboBox<MenuAdapter>(menuXScale);
		unitYCB = new JComboBox<MenuAdapter>(menuYScale);
		ComboBoxRenderer renderer = new ComboBoxRenderer();
		unitXCB.setRenderer(renderer);
		unitYCB.setRenderer(renderer);
		JLabel unitXL = new JLabel(MenuProperty.CHART_XSCALE.getLabel());
		JLabel unitYL = new JLabel(MenuProperty.CHART_YSCALE.getLabel());

		
		unitXMax = new JFormattedTextField(numberFormat);
		unitXMax.setColumns(20);
		unitXMin = new JFormattedTextField(numberFormat);
		unitXMin.setColumns(20);
		unitYMax = new JFormattedTextField(numberFormat);
		unitYMax.setColumns(20);
		unitYMin = new JFormattedTextField(numberFormat);
		unitYMin.setColumns(20);
		
		JPanel p = new JPanel(new GridBagLayout());
		p.setBorder(BorderFactory.createTitledBorder(MenuProperty.CHART_AXIS_SETTING.getLabel()));
		GridBagConstraints c = new GridBagConstraints();

		c.insets = new Insets(2, 2, 2, 2);
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.EAST;
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.NONE;
		p.add(unitXL, c);
		c.gridx = 1;
		c.fill = GridBagConstraints.BOTH;
		p.add(unitXCB, c);
		c.gridx = 0;
		c.gridy = 1;
		c.fill = GridBagConstraints.NONE;
		p.add(new JLabel("X Min"), c);
		c.gridx = 1;
		c.fill = GridBagConstraints.BOTH;
		p.add(unitXMin, c);
		c.gridx = 0;
		c.gridy = 2;
		c.fill = GridBagConstraints.NONE;
		p.add(new JLabel("X Max"), c);
		c.gridx = 1;
		c.fill = GridBagConstraints.BOTH;
		p.add(unitXMax, c);

		c.gridx = 0;
		c.gridy = 3;
		c.fill = GridBagConstraints.NONE;
		p.add(unitYL, c);
		c.gridx = 1;
		c.fill = GridBagConstraints.BOTH;
		p.add(unitYCB, c);
		c.gridx = 0;
		c.gridy = 4;
		c.fill = GridBagConstraints.NONE;
		p.add(new JLabel("Y Min"), c);
		c.gridx = 1;
		c.fill = GridBagConstraints.BOTH;
		p.add(unitYMin, c);
		c.gridx = 0;
		c.gridy = 5;
		c.fill = GridBagConstraints.NONE;
		p.add(new JLabel("Y Max"), c);
		c.gridx = 1;
		c.fill = GridBagConstraints.BOTH;
		p.add(unitYMax, c);
		return p;
	}

	public boolean isEligibleOnly() {
		return eligibleCB.isSelected();
	}

	public void setCostBenefitUnit(CostBenefitUnit cbu) {
		cbuCB.setSelectedItem(cbu);
	}

	public void setEligibleOnly(boolean b) {
		eligibleCB.setSelected(b);
	}

	public void setProject(Project project) {
		this.project = project;
		for (CostBenefitUnit cbu : project.getCostBenefitUnitList()) {
			cbuModelCB.addElement(cbu);
		}
	}

	public void setUnitX(MenuProperty m) {
		unitXCB.setSelectedItem(m);
	}

	public void setUnitXMax(String unitXMax) {
		this.unitXMax.setText(unitXMax);
	}

	public void setUnitXMin(String unitXMin) {
		this.unitXMin.setText(unitXMin);
	}

	public void setUnitY(MenuProperty m) {
		unitYCB.setSelectedItem(m);
	}

	public void setUnitYMax(String unitYMax) {
		this.unitYMax.setText(unitYMax);
	}

	public void setUnitYMin(String unitYMin) {
		this.unitYMin.setText(unitYMin);
	}

	public String getThreshold() {
		return costF.getText();
	}
	
	public void setThreshold(String s) {
		this.costF.setText(s);
	}

}
